#define _CRT_SECURE_NO_WARNINGS 1

#include"CommentConvert.h"

void CommentConvert(FILE *pfIn, FILE *pfOut)
{
	void DoConverWork(FILE *pfIn, FILE *pfOut);
	DoConverWork(pfIn, pfOut);
}
void DoConverWork(FILE *pfIn, FILE *pfOut)
{
	enum STATE state = NUL_STATE;
	while (state != END_STATE)
	{
		switch (state)
		{
		case NUL_STATE:
			Do_NUL_STATE(pfIn, pfOut, &state);
			break;
		case C_STATE:
			Do_C_STATE(pfIn, pfOut, &state);
			break;
		case CPP_STATE:
			Do_CPP_STATE(pfIn, pfOut, &state);
			break;
		default:
			break;
		}
	}
}

void Do_NUL_STATE(FILE *pfIn, FILE *pfOut, enum STATE *state)
{
	int first = 0;
	int second = 0;
	first = fgetc(pfIn);
	switch (first)
	{
	case '/':
	{
				second = fgetc(pfIn);
				switch (second)
				{
				case '*':
				{
							fputc('/', pfOut);
							fputc('/', pfOut);
							*state = C_STATE;
				}
					break;
				case '/':
				{
							fputc(first, pfOut);
							fputc(second, pfOut);
							*state = CPP_STATE;
				}
					break;
				default:
				{
						   fputc(first, pfOut);
						   fputc(second, pfOut);
				}
					break;
				}
	}
		break;
	case EOF:
		*state = END_STATE;
		break;
	default:
		fputc(first, pfOut);
		break;
	}
}

void Do_C_STATE(FILE *pfIn, FILE *pfOut, enum STATE *state)
{
	int first = 0;
	int second = 0;
	int third = 0;
	first = fgetc(pfIn);
	switch (first)
	{
	case '*':
	{
				second = fgetc(pfIn);
				switch (second)
				{
				case '/':
				{
							third = fgetc(pfIn);
							if (third != '\n')
							{
								fputc('\n', pfOut);
							}
							if (third == '/')
							{
								ungetc(third, pfIn);
								*state = NUL_STATE;
								break;
							}
							fputc(third, pfOut);
							*state = NUL_STATE;
				}
					break;
				case '*':
				{
							fputc(first, pfOut);
							third = fgetc(pfIn);
							if (third == '/')
							{
								*state = NUL_STATE;
								break;
							}
							fputc(second, pfOut);
							ungetc(third, pfIn);
				}
					break;
				default:
				{
						   fputc(first, pfOut);
						   fputc(second, pfOut);
				}
					break;
				}
	}
		break;
	case '\n':
	{
				 fputc(first, pfOut);
				 fputc('/', pfOut);
				 fputc('/', pfOut);
	}
		break;
	case EOF:
		*state = END_STATE;
		break;
	default:
		fputc(first, pfOut);
		break;
	}
}

void Do_CPP_STATE(FILE *pfIn, FILE *pfOut, enum STATE *state)
{
	int first = 0;
	first = fgetc(pfIn);
	switch (first)
	{
	case '\n':
	{
				 fputc(first, pfOut);
				 *state = NUL_STATE;
	}
		break;
	case EOF:
		*state = END_STATE;
		break;
	default:
		fputc(first, pfOut);
		break;
	}
}